package com.ruoyi.web.controller.trade;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.dto.FeatureDTO;
import com.hundun.vision.api.dto.FeatureTemplateDTO;
import com.hundun.vision.api.query.FeatureQuery;
import com.hundun.vision.api.request.FeatureRequest;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.FeatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：20:24 2021/3/2
 * @description：特征控制器
 */
@RestController
@RequestMapping("/trade/feature")
public class FeatureController extends BaseController {

    @Autowired
    private FeatureService featureService;

    @PreAuthorize("@ss.hasPermi('trade:feature:save')")
    @Log(title = "特征管理", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody FeatureRequest request) {
        return toAjax(featureService.save(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:feature:remove')")
    @Log(title = "特征管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{featureIds}")
    public AjaxResult remove(@PathVariable List<Long> featureIds) {
        return toAjax(featureService.removeByIds(featureIds));
    }

    @PreAuthorize("@ss.hasPermi('trade:feature:update')")
    @Log(title = "特征管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody FeatureRequest request) {
        return AjaxResult.success(featureService.update(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:feature:get')")
    @GetMapping(value = {"/", "/{featureId}"})
    public AjaxResult get(@PathVariable(value = "featureId", required = false) Long featureId) {
        AjaxResult ajax = AjaxResult.success();
        List<FeatureTemplateDTO> featureTemplateDTOS = featureService.getFeatureTemplates();
        ajax.put("featureTemplates", featureTemplateDTOS);
        if (StringUtils.isNotNull(featureId)) {
            ajax.put(AjaxResult.DATA_TAG, featureService.getById(featureId));
        }
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('trade:feature:search')")
    @GetMapping("/search")
    public TableDataInfo search(FeatureQuery query) {
        PageInfo<FeatureDTO> pageInfo = featureService.search(query);
        return getDataTable(pageInfo);
    }
}
